//
//  PPActivityViewController.m
//  PetParadise
//
//  Created by 陈无忌 on 9/6/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPActivityViewController.h"
#import "PPActivityDetailViewController.h"
#import "supportingHeadFiles.h"
#import "PPSystemConfiguraton.h"
#import "PPActivity.h"
#import "PPCustomBGTableView.h"
#import "PPActivityTableViewCell.h"

@interface PPActivityViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet PPCustomBGTableView *activityListTableView;
@property (nonatomic, strong) NSMutableArray *activityArray;

@end

@implementation PPActivityViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getActivity];
    self.activityListTableView.dataSource = self;
    self.activityListTableView.delegate = self;
    // Do any additional setup after loading the view.
}

-(void)getActivity
{
    NSString *url = [PPAPIList getAPIList].PPQueryAllNotification;
    NSDictionary *params = @{@"id":@0};
    [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
        NSLog(@"%@",responseObject);
        self.activityArray = [[NSMutableArray alloc] init];
        NSDictionary *resultMap = responseObject[@"resultMap"];
        NSArray *activityList = resultMap[@"notificationList"];
        for (NSDictionary *dic in activityList) {
            NSError *err =nil;
            PPActivity *activity = [[PPActivity alloc] initWithDictionary:dic error:&err];
            [self.activityArray addObject:activity];
        }
        [self.activityListTableView reloadData];
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.activityArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PPActivityTableViewCell *cell = [self.activityListTableView dequeueReusableCellWithIdentifier:@"activityCell"];
    PPActivity *activity = [self.activityArray objectAtIndex:indexPath.row];
    [cell.bannerImageView setImageWithURL:[PPImageUrl getImageUrlOriginal:activity.imgUrl] placeholderImage:[UIImage imageNamed:@"defaultImage"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    cell.titleLabel.text = activity.title;
    [cell.bannerImageView setContentMode:UIViewContentModeScaleAspectFill];
    [cell.bannerImageView setClipsToBounds:YES];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PPActivity *activity = [self.activityArray objectAtIndex:indexPath.row];
    PPActivityDetailViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"activityDetailVC"];
    targetVC.activity = activity;
    [self.navigationController pushViewController:targetVC animated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
